package com.hht.wblib.utils;

import android.content.Context;

import com.hht.wblib.bean.UniversalBean;
import com.hht.wblib.hht.HhtWhiteBoard;
import com.hht.wblib.ist.IstWhiteBoard;
import com.hht.wblib.lg.LgWhiteBoard;
import com.hht.wblib.optoma.OptomaWhiteBoard;

import java.io.File;

/**
 * Author:chen'hu
 * Time:2025/1/3 10:36
 * Description:当获取系统配置失败，启用本地备用方法
 */
public class UniversalHelp {

    private Context mContext;
    private HhtWhiteBoard mOemWhiteBoard;
    private HhtWhiteBoard mNewlineWhiteBoard;
    private HhtWhiteBoard mNewlineUpdateWhiteBoard;
    private IstWhiteBoard mIstWhiteBoard;
    private LgWhiteBoard mLgWhiteBoard;
    private OptomaWhiteBoard mOptomaWhiteBoard;
    private static volatile UniversalHelp INSTANCE;

    /**
     * @param context 上下文对象
     */
    private UniversalHelp(Context context) {
        this.mContext = context.getApplicationContext();
        this.mOemWhiteBoard = new HhtWhiteBoard(mContext, UniversalBean.getInstance().getHhtOemConfigurationBean());
        this.mNewlineWhiteBoard = new HhtWhiteBoard(mContext, UniversalBean.getInstance().getHhtNewLineConfigurationBean());
        this.mNewlineUpdateWhiteBoard = new HhtWhiteBoard(mContext, UniversalBean.getInstance().getHhtNewUpdateLineConfigurationBean());
        this.mIstWhiteBoard = new IstWhiteBoard(mContext, UniversalBean.getInstance().getIstConfigurationBean());
        this.mLgWhiteBoard = new LgWhiteBoard(mContext, UniversalBean.getInstance().getLgConfigurationBean());
        this.mOptomaWhiteBoard = new OptomaWhiteBoard(mContext, UniversalBean.getInstance().getOptomaConfigurationBean());
    }

    /**
     * @param context 上下文对象
     */
    public static UniversalHelp getInstance(Context context) {
        if (null == INSTANCE) {
            synchronized (UniversalHelp.class) {
                if (null == INSTANCE) {
                    INSTANCE = new UniversalHelp(context);
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 因为启动的是service，并没有返回值，所以默认全部启动一次
     */
    public boolean openAnnotation() {
        if (mNewlineWhiteBoard.openAnnotation()) {
            return true;
        }
        mNewlineUpdateWhiteBoard.openAnnotation();
        mOemWhiteBoard.openAnnotation();
        mIstWhiteBoard.openAnnotation();
        mLgWhiteBoard.openAnnotation();
        mOptomaWhiteBoard.openAnnotation();
        return true;
    }


    /**
     * 尝试依次启动不同厂商的白板，直到有一个成功启动。
     * 如果所有尝试都失败，则返回 false；如果任何一个尝试成功，则返回 true。
     */
    public boolean openWhiteBoard() {
        // 尝试打开HHT白板
        if (mOemWhiteBoard.openWhiteBoard()) {
            return true;
        }
        if (mNewlineWhiteBoard.openWhiteBoard()) {
            return true;
        }
        // 如果HHT白板打开失败，尝试打开IST白板
        if (mIstWhiteBoard.openWhiteBoard()) {
            return true;
        }
        // 如果IST白板也打开失败，尝试打开LG白板
        if (mLgWhiteBoard.openWhiteBoard()) {
            return true;
        }
        // 如果上述白板均打开失败，尝试打开Optoma白板，并返回其结果
        return mOptomaWhiteBoard.openWhiteBoard();
    }


    /**
     * @param file 尝试依次向不同厂商的白板发送图片，直到有一个成功。
     *             如果所有尝试都失败，则返回 false；如果任何一个尝试成功，则返回 true。
     */
    public boolean sendPicturesToWhiteBoard(File file) {
        // 尝试向HHT白板发送图片
        if (mOemWhiteBoard.sendPicturesToWhiteBoard(file)) {
            return true;
        }
        if (mNewlineWhiteBoard.sendPicturesToWhiteBoard(file)) {
            return true;
        }
        // 如果HHT白板发送失败，尝试向IST白板发送
        if (mIstWhiteBoard.sendPicturesToWhiteBoard(file)) {
            return true;
        }
        // 如果IST白板也发送失败，尝试向LG白板发送
        if (mLgWhiteBoard.sendPicturesToWhiteBoard(file)) {
            return true;
        }
        // 如果上述白板均发送失败，尝试向Optoma白板发送，并返回其结果
        return mOptomaWhiteBoard.sendPicturesToWhiteBoard(file);
    }

}
